package com.thwj.project.biz.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.Page;
import com.thwj.common.annotation.Log;
import com.thwj.common.core.controller.BaseController;
import com.thwj.common.core.domain.AjaxResult;
import com.thwj.common.core.page.TableDataInfo;
import com.thwj.common.enums.BusinessType;
import com.thwj.common.utils.poi.ExcelUtil;
import com.thwj.project.biz.controller.vo.UserAccountWithdrawVO;
import com.thwj.project.biz.convert.UserInfoAdminResolver;
import com.thwj.project.biz.domain.UserAccountWithdraw;
import com.thwj.project.biz.service.IUserAccountWithdrawService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 用户提现Controller
 *
 * @author generate
 * @date 2023-06-16
 */
@RestController
@RequestMapping("/biz/withdraw")
public class UserAccountWithdrawController extends BaseController {
    @Autowired
    private IUserAccountWithdrawService userAccountWithdrawService;
    @Autowired
    private UserInfoAdminResolver userInfoAdminResolver;

/**
 * 查询用户提现列表
 */
@PreAuthorize("@ss.hasPermi('biz:withdraw:list')")
@GetMapping("/list")
    public TableDataInfo list(UserAccountWithdraw userAccountWithdraw) {
        startPage();
        List<UserAccountWithdraw> list = userAccountWithdrawService.getBaseMapper().selectList(new QueryWrapper<>(userAccountWithdraw));

        //
        List<UserAccountWithdrawVO> withdrawVOS = list.stream().map(item->{
            UserAccountWithdrawVO withdrawVO = new UserAccountWithdrawVO();
            BeanUtils.copyProperties(item,withdrawVO);
            return withdrawVO;
        }).collect(Collectors.toList());
        userInfoAdminResolver.resolve(withdrawVOS,UserAccountWithdrawVO::getUserId,(obj, nickname, phone)->{
            obj.setNickName(nickname);
            obj.setUserPhone(phone);
        });

    return getDataTable(withdrawVOS,((Page)list).getTotal());
}

    /**
     * 导出用户提现列表
     */
    @PreAuthorize("@ss.hasPermi('biz:withdraw:export')")
    @Log(title = "用户提现", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, UserAccountWithdraw userAccountWithdraw) {
        List<UserAccountWithdraw> list = userAccountWithdrawService.list(new QueryWrapper<>(userAccountWithdraw));
        ExcelUtil<UserAccountWithdraw> util = new ExcelUtil<UserAccountWithdraw>(UserAccountWithdraw. class);
        util.exportExcel(response, list, "用户提现数据");
    }

    /**
     * 获取用户提现详细信息
     */
    @PreAuthorize("@ss.hasPermi('biz:withdraw:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(userAccountWithdrawService.getById(id));
    }

    /**
     * 新增用户提现
     */
    @PreAuthorize("@ss.hasPermi('biz:withdraw:add')")
    @Log(title = "用户提现", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody UserAccountWithdraw userAccountWithdraw) {
        return toAjax(userAccountWithdrawService.save(userAccountWithdraw));
    }

    /**
     * 修改用户提现
     */
    @PreAuthorize("@ss.hasPermi('biz:withdraw:edit')")
    @Log(title = "用户提现", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody UserAccountWithdraw userAccountWithdraw) {
        return toAjax(userAccountWithdrawService.updateById(userAccountWithdraw));
    }

    /**
     * 删除用户提现
     */
    @PreAuthorize("@ss.hasPermi('biz:withdraw:remove')")
    @Log(title = "用户提现", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(userAccountWithdrawService.removeByIds(Arrays.asList(ids)));
    }

    /**
     * 提现审核成功
     */
    @PreAuthorize("@ss.hasPermi('biz:withdraw:edit')")
    @Log(title = "审核提现成功", businessType = BusinessType.UPDATE)
    @PostMapping("auditPass")
    public AjaxResult auditPass(@RequestBody UserAccountWithdraw userAccountWithdraw) {
        return toAjax(userAccountWithdrawService.auditPass(userAccountWithdraw));
    }

    /**
     * 提现审核不通过
     */
    @PreAuthorize("@ss.hasPermi('biz:withdraw:edit')")
    @Log(title = "审核提现不通过", businessType = BusinessType.UPDATE)
    @PostMapping("auditNotPass")
    public AjaxResult auditNotPass(@RequestBody UserAccountWithdraw userAccountWithdraw) {
        return toAjax(userAccountWithdrawService.auditNotPass(userAccountWithdraw));
    }
}
